package main;
//package db;
//package com.tbilgazi.billing.db.common;

//ttttttttttttttt

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.*;
import javax.sql.*;
//import com.tbilgazi.billing.gui.Lingvo;
//import params.*;
//import com.tbilgazi.billing.gui.auth.titles;

import java.util.*;

import java.text.*;


public class functions {
    
   public static Connection GetMyConnection() {
//       System.out.println("getmy1111111111111111");
        try {
            Class.forName(sets.db_driver);
        } catch (Exception e) {
            System.out.println("111111111" + e.toString());
            return null;
        }
        //            DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
        try {
            //            DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
            return DriverManager.getConnection(sets.db_string, sets.db_user,
                    sets.db_pass);
        } catch (Exception e) {
            System.out.println("22222222" + e.toString());
            return null;
        }
    }

  
 
    public static String execSqlProcedure(String procedureName) {
        
        Connection connection = null;
        CallableStatement pstmt=null;
        
        try {
            
            connection = GetMyConnection();
            pstmt = connection.prepareCall("{? = call " + procedureName);
            pstmt.registerOutParameter(1, Types.INTEGER);
            pstmt.execute();
            
            String result = pstmt.getString(1);
            return result;
            
        } catch (SQLException e) {
            
            e.printStackTrace();
            return e.toString();
            
        }finally {
            try { pstmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        
    }
    
    public static String execFunction(String in_function, String[] in_values) {
        return execFunction(in_function,in_values,true);
    }
    public static String execFunction(String in_function, String[] in_values,boolean sysout) {
        Connection connection = null;
        CallableStatement pstmt=null;
        try {
            
            connection = GetMyConnection();
            //String [] temp = null;
            //temp = in_values.split("\\,");
            String tmpX = new String("");
            //                            System.out.println("Lengh: " + in_values.length);
            String cs = "";
            if (in_values == null)
                tmpX = "";
            else {
                //                            System.out.println("Lengh: " + in_values.length);
                for (int i = 1; i < in_values.length; i++) {
                    tmpX += "?,";
                    
                }
                tmpX += "?";
            }
            pstmt =
                    connection.prepareCall("{? = call " + in_function + "(" + tmpX + ")}");
System.out.println("{? = call " + in_function + "(" + tmpX + ")}");            
            pstmt.registerOutParameter(1, Types.INTEGER);
            if (in_values != null)
                for (int i = 0; i < in_values.length; i++) {
                pstmt.setString(i + 2, in_values[i]);
//                pstmt.setInt(i + 2, str2int0(in_values[i]));
                cs += "," + in_values[i];
                }
            if (sysout) System.out.println("procedure=" + in_function + cs);
            pstmt.execute();
                                 String ss=String.format("%s",pstmt.getInt(1));
            //String ss = pstmt.getString(1);

            return ss;
        } catch (SQLException e) {
            e.printStackTrace();
            return e.toString();
        }finally {
            try { pstmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        //        return "true";
    }
    
    public static String execSql(String sql) {
        Connection connection = null; Statement stmt=null;
        try {
            //                table_props.gettable_names(in_statement);
            connection = GetMyConnection();
            stmt = connection.createStatement();
            //stmt.executeQuery(sql);
            stmt.execute(sql);
            connection.close();
        } catch (SQLException e) {
            
            e.printStackTrace();
            return e.toString();
            
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        return "0";
    }
    
 /*
        public static void getResultset(String sql,Table_props table_props) {
        System.out.println(sql);
        table_props.rs=null;
        try {
            
            table_props.con = GetMyConnection();
            table_props.stmt =
                    table_props.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            table_props.rs = table_props.stmt.executeQuery(sql);

            
            return;
            
            
        } catch (SQLException e) {
            try { table_props.stmt.close();      } catch (Exception e2) {}
            try { table_props.con.close();} catch (Exception e2) {}
            System.out.println("***: " + e.toString());
        }
        
    }
   */
    
    
    public static String[][] getResult(String sql) {
        System.out.println(sql);
        String[][] retVal;
        Connection connection = null;Statement stmt =null;
        try {
            
           
            
            connection = GetMyConnection();
            stmt =
                    connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(sql);
            int columnCount = rs.getMetaData().getColumnCount();
            int rowCount = 0;
            
            while (rs.next())
                rowCount++;
            rs.beforeFirst();
            
            retVal = new String[rowCount][columnCount];
            
            int i = 0;
            
            while (rs.next()) {
                
                for (int k = 1; k <= columnCount; k++) {
                    retVal[i][k - 1] = rs.getString(k);
                }
                i++;
                
            }
            
           
            
            return retVal;
            
            
        } catch (SQLException e) {
            System.out.println("***: " + e.toString());
            
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
         return null;
    }
    public static StringBuffer getResult2(String sql,String nn,String tt) {
        System.out.println(sql);
        StringBuffer retVal=new StringBuffer("");
        Connection connection = null;Statement stmt =null;
        try {

            connection = GetMyConnection();
            stmt =
                    connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(sql);
            int columnCount = rs.getMetaData().getColumnCount();
      /*      int rowCount = 0;

            while (rs.next())
                rowCount++;
            rs.beforeFirst();*/

//            retVal = new String[rowCount][columnCount];

//            int i = 0;

            while (rs.next()) {
                for (int k = 1; k <= columnCount; k++) {
                    retVal.append(rs.getString(k)+tt);
                }
                retVal.append(nn);
  //              i++;

            }



            return retVal;


        } catch (SQLException e) {
            System.out.println("***: " + e.toString());

        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
         return null;
    }
    public static String getTableNames(String name) {
        String sql = "select * from " + name+" limit 1";
        System.out.println(sql);
        Connection connection = null; Statement stmt =null;
        String retVal = "";
        try {
            
            connection = GetMyConnection();
            stmt =
                    connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery(sql);
            int columnCount = rs.getMetaData().getColumnCount();
            for (int k = 0; k < columnCount; k++)
                if (k == 0)
                    retVal = rs.getMetaData().getColumnName(k + 1);
                else
                    retVal += "," + rs.getMetaData().getColumnName(k + 1);
            
            
           
            
            return retVal;
            
            
        } catch (SQLException e) {
            System.out.println("***: " + e.toString());
            
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
          return null;
    }
    
    public static String deleteFunction(String in_statement, String idName,
            String uid) {
        Connection connection = null;Statement stmt =null;
        try {
            //                table_props.gettable_names(in_statement);
            connection = GetMyConnection();
            stmt = connection.createStatement();
            String sql =
                    "delete from " + in_statement + " where " + idName + "=" + uid;
            //stmt.executeQuery(sql);
            boolean x = stmt.execute(sql);
            if (x)
                return "error";
            
        } catch (SQLException e) {
            return e.toString();
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        return "1";
    }
    /*    public static String applyFunction(String in_statement, String[] in_values) {
//update "dbo"."oraAccSources" set sourcefile='find_customer',href='find_customer' where id=34
        Connection connection = null;
            try {
//                table_props.gettable_names(in_statement);
                connection = GetMyConnection();
                Statement stmt = connection.createStatement();
                String idName=table_props.names[0];
                String idVal=in_values[0];
     
                String sql="update "+in_statement+" set ";
boolean da=false;
     
for (int ii=1;ii<in_values.length;ii++)
{
    String nam=table_props.names[ii];
    String val = in_values[ii];
     
    if ((val!=null)&&(val.length()!=0))
    {
        if (da)
            sql+=String.format(",%s='%s'", nam, val);
        else
            sql+=String.format("%s='%s'", nam, val);
        da=true;
    }
}
 sql+=" where "+idName+"="+idVal;
                stmt.executeQuery(sql);
                connection.close();
            }
            catch (SQLException e) {
                return e.toString();
            }
        return "true";
    }
     */
    
    
    public static java.util.Date getServerTime() {
        Connection con = null;Statement stmt =null;
        java.util.Date ret = new java.util.Date();
        try {
            con = GetMyConnection();
            String sql = "SELECT GETDATE() ";
             stmt =con.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            if (rs.next()) {
                java.sql.Date d = rs.getDate(1);
                ret.setTime(d.getTime());
            }
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
finally {
            try { stmt.close();      } catch (Exception e) {}
            try { con.close();} catch (Exception e) {}

        }
            
        
        return ret;
        
    }
    
    public static String updateFunction(String in_statement,
            String[] table_names,
            String[] in_values) {
        return updateFunction(in_statement, table_names, in_values, "-1");
    }
    
    public static String updateFunction(String in_statement,
            String[] table_names,
            String[] in_values, String where) {
        
        Connection connection = null;Statement stmt =null;
        String sql = "";
        try {
            //                table_props.gettable_names(in_statement);
            connection = GetMyConnection();
             stmt = connection.createStatement();
            
            sql = "update " + in_statement + " set ";
            boolean da = false;
            
            for (int ii = 1; ii < in_values.length; ii++) {
                String nam = table_names[ii];
                String val = in_values[ii];
                val = functions.conv_UTF8_2_char(val);
                boolean am=nam.equalsIgnoreCase("amount");
                if ((val != null) && (val.length() != 0)) {
                    if (am) {
                        if (da)
                            sql += String.format(",%s=%s", nam, val);
                        else
                            sql += String.format("%s=%s", nam, val);
                    }else {
                        if (da)
                            sql += String.format(",%s='%s'", nam, val);
                        else
                            sql += String.format("%s='%s'", nam, val);
                    }
                    da = true;
                } else {
                    if (da)
                        sql += String.format(",%s=null", nam);
                    else
                        sql += String.format("%s=null", nam);
                    da = true;
                    
                }
            }
            sql += " where " + table_names[0] + "=" + in_values[0];
            
            if (!where.equals("-1")) {
                sql += (" and " + where);
            }
            
            System.out.println("update sql=" + sql);
            //                stmt.executeQuery(sql);
            boolean x = stmt.execute(sql);
            if (x)
                return "error";
            
           
        } catch (SQLException e) {
            return sql + e.toString();
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        return "1";
    }
    
    public static String insertFunction(String in_statement,
            String[] table_names,
            String[] in_values) {
        
        String sql = "";
        
        Connection connection = null;Statement stmt =null;
        try {
            //                table_props.gettable_names(in_statement);
            connection = GetMyConnection();
            stmt = connection.createStatement();
            
            String lin1 = "";
            String lin2 = "";
            boolean da = false;
            for (int ii = 0; ii < in_values.length; ii++) {
                String nam = table_names[ii];
                String val = in_values[ii];
                val = functions.conv_UTF8_2_char(val);
                if (val != null && val.length() != 0) {
                    if (da) {
                        lin1 += "," + nam;
                        lin2 += ",'" + val + "'";
                    } else {
                        lin1 += nam;
                        lin2 += "'" + val + "'";
                    }
                    da = true;
                }
            }
            
            sql =
                    "insert into " + in_statement + String.format(" (%s) values (%s)", lin1,
                    lin2);
            System.out.println(sql);
            
            //                stmt.executeQuery(sql);
            boolean x = stmt.execute(sql);
            if (x)
                return "error";
            
           
        } catch (SQLException e) {
            System.out.println(sql + e.toString());
            return sql + e.toString();
        }finally {
            try { stmt.close();      } catch (Exception e) {}
            try { connection.close();} catch (Exception e) {}

        }
        return "1";
    }
        static public String conv2_UTF(String ss)
    {
        try
        {
        String cs=new String(ss.getBytes("ISO-8859-1"),"utf-8");
        return cs;
        }catch (Exception e)
        {
            return ss;
        }
     }
    
    public static String[] conv_UTF8_2_char(String[] ss) {
        String[] rez = new String[ss.length];
        for (int i = 0; i < ss.length; i++) {
            rez[i] = conv_UTF8_2_char(ss[i]);
        }
        return rez;
    }
    
    public static String conv_UTF8_2_char(String ss) {
         return conv_UTF8_2_UTF(ss);
        //return conv_UTF__2_char(ss);
    }
    
    public static String conv_UTF8__2_char(String ss) {
        if (ss == null)
            return null;
        System.out.println("=========================");
        System.out.println(ss.length());
        System.out.println(ss);
        return ss;
   /*     
        StringBuffer cs = new StringBuffer("");
        
        for (int i = 0; i < ss.length(); i++) {
            char cc = ss.charAt(i);
            byte hi = (byte)(cc >>> 8);
            if (hi == 0x10) {
                int lo = cc & 0xff;
                int lo1 = lo;
                if ((lo >= 0xD0) && (lo <= 0xD6))
                    lo1 = lo - 16;
                else if ((lo >= 0xD7) && (lo <= 0xDC))
                    lo1 = lo - 15;
                else if ((lo >= 0xDD) && (lo <= 0xE2))
                    lo1 = lo - 14;
                else if ((lo >= 0xE3) && (lo <= 0xEE))
                    lo1 = lo - 13;
                else if ((lo >= 0xEF) && (lo <= 0xF0))
                    lo1 = lo - 12;
                cs.append((char)lo1);
            } else
                cs.append(cc);
        }
        return cs.toString();*/
    }
    
    public static String conv_UTF8_2_UTF(String input) {
//     return input;
        if (input == null) {
            return "";
        }
        if (input == null && input.trim().length() == 0) {
            return "";
        }     
   
 /*       String s1=conv2_UTF(input);
   System.out.println("==================="+input);     
   System.out.println("==============="+s1);   
   return s1;*/
   
        StringBuffer output = new StringBuffer("");
        
        
        
        for (int i = 0; i < input.length(); i++) {
            
            if ((i + 7) > input.length()) {
                output.append( input.substring(i) );
                break;
            }
            
            if (input.substring(i, i + 1).equals("&") &&
                    input.substring(i + 1, i + 2).equals("#") &&
                    input.substring(i + 6, i + 7).equals(";")) {
                
                try {
                    
                    output.append( (char)Integer.parseInt(input.substring(i + 2, i + 6)) );
                    i += 6;
                    
                } catch (Exception e) {
                    
                    System.out.println(" ================================================================================== ");
                    System.out.println(" ===================== EXCEPTION IN functions.conv_UTF8_2_UTF ===================== ");
                    e.printStackTrace();
                    System.out.println(" ================================================================================== ");
                    
                }
                
            } else {
                
                output.append( input.substring(i, i + 1) );
                
            }
            
        }
         
        return output.toString();
        
    }
    
    public static String conv_utf_2_UTF8(String ss) {
        return ss;
/*        if (ss == null)
            return null;
        StringBuffer cs = new StringBuffer("");
        for (int i = 0; i < ss.length(); i++) {
            char cc = ss.charAt(i);
            
            int ii = (int)(cc);
            
            if (ii < 256) {
                cs.append(cc);
                continue;
            }
            cs.append(String.format("&#%d;", ii));
        }
        return cs.toString();*/
    }
    //////////////////////////////////////////
    
   
    /*    static public String[] gettitles(String in_statement)
    {
        String cs=gettitle(in_statement);
        if (cs.equals("")) return null;
        if (cs.indexOf(",")<0) return null;
        return cs.split(",");
    }
    static public String[] gettitlespro(String[] in_statement)
    {
        if (in_statement==null) return null;
        String[] cs=new String[in_statement.length];
        for (int i=0;i<in_statement.length;i++)
            cs[i]=gettitle(in_statement[i]);
//        if (cs.equals("")) return null;
        return cs;
    }
    static public String gettitle(String in_statement)
    {
        String ss="";
//        try
  //      {
    //       ss=titles.messages.getString(in_statement);
      //  }catch (Exception ee)
        //{
            ss=in_statement;
        //}
        return ss;
    }*/
    
    public static String selectcombo(String s1, String s2) {
        if (s1.equals(s2))
            return " selected='selected' ";
        return "";
    }
    
    public static String inserttext(String da1, String da2, int i,
            String strda, String strno) {
        if (da1 == null) {
            if (i == 1)
                return strda;
            else
                return strno;
        }
        if (da1.equals(da2))
            return strda;
        return strno;
    }
    
    public static String inserttext(boolean da, String strda, String strno) {
        if (da)
            return strda;
        else
            return strno;
    }
    
   
    
    ////////////////////////////////////////////////////////////////////////
    
    public static String chis2(String val, boolean withComma) {
        Double xx;
        try {
            xx = Double.valueOf(val);
        } catch (Exception e) {
            return val;
        }
        String pattern = "";
        pattern = withComma ? "%,.2f" : "%.2f";
        
        return String.format(pattern, xx);
    }
    
    public static String chis2(Double xx) {
        return String.format("%,.2f", xx);
    }
    
    public static String chis2(String val) {
        return chis2(val, true);
    }
    
    ////////////////////////////////////////////////////////////////////////
    
    public static String chis0(String val, boolean withComma) {
        Double xx;
        try {
            xx = Double.valueOf(val);
        } catch (Exception e) {
            return val;
        }
        
        String pattern = "";
        pattern = withComma ? "%,.0f" : "%.0f";
        
        return String.format(pattern, xx);
    }
    
    public static String chis0(double xx) {
        return String.format("%,.0f", xx);
    }
    
    public static String chis0(String val) {
        return chis0(val, true);
    }
    
   
    public static String getnow() {
        Calendar calendar = new GregorianCalendar();
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
        return ff.format(date);
    }
    public static long getdeltanow(String ss) {

        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
         java.util.Date today = new  java.util.Date();


try{
         java.util.Date d0 = ff.parse("2000-1-1");
        java.util.Date d1 = ff.parse(ss);
          long diff = (today.getTime()-d0.getTime()) / (1000 * 60 * 60 * 24);
          long diff1 = (d1.getTime()-d0.getTime()) / (1000 * 60 * 60 * 24);
          System.out.println("diff="+(diff-diff1) );
          return diff-diff1 ;
}catch(Exception e){
    e.printStackTrace();
    return 1000;
}


    }
    public static String getnowdatetime() {
        Calendar calendar = new GregorianCalendar();
        java.util.Date date = calendar.getTime();
        //        System.out.println("-------------"+date.toString());
        //        private DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.LONG, locale);
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return ff.format(date);
    }
    
    public static String getnow2() {
        
        /*        Calendar calendar = new GregorianCalendar();
        Integer year = new Integer(calendar.get(Calendar.YEAR));
        Integer month = new Integer(calendar.get(Calendar.MONTH));
        Integer day = new Integer(calendar.get(Calendar.DAY_OF_MONTH));
        return year.toString() + "-" + month.toString() + "-" + day.toString();*/
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.MONTH, -1);
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
        return ff.format(date);
        
    }
    
    public static String getnow3(int day) {
        
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, day);
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
        return ff.format(date);
        
    }
    
    public static String getnowzavtra() {
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
        return ff.format(date);
        
    }
    public static String getnownextmonth() {
        Calendar calendar = new GregorianCalendar();
        calendar.add(Calendar.MONTH, 1);
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
        return ff.format(date);
        
    }    
    public static String getnow1() {
        Calendar calendar = new GregorianCalendar();
        java.util.Date date = calendar.getTime();
        DateFormat localFormat = DateFormat.getDateInstance();
        DateFormat ff = new SimpleDateFormat("yyyy-MM-01");
        return ff.format(date);
    }
    
    public static String getnow1ianv() {
        
        Calendar calendar = new GregorianCalendar();
        Integer year = new Integer(calendar.get(Calendar.YEAR));
        return year.toString() + "-01-01";
        
    }
    
    public static String getnow31dek() {
        
        Calendar calendar = new GregorianCalendar();
        Integer year = new Integer(calendar.get(Calendar.YEAR));
        return year.toString() + "-12-31";
        
    }
    
    public static String getdate_mes(String dat, int mes) {
        DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date myDate = null;
        try {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(myDateFormat.parse(dat));
            calendar.add(Calendar.MONTH, mes);
            return myDateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            return "";
        }
        
    }
    
    public static Integer str2int(String str) {
        Integer rr = 0;
        try {
            rr = Integer.parseInt(str);
        } catch (Exception e) {
            // rr=-111111;
            rr = null;
        }
        return rr;
    }
    
    public static int str2int0(String str) {
        int rr = 0;
        try {
            rr = Integer.parseInt(str);
        } catch (Exception e) {
            // rr=-111111;
            rr = 0;
        }
        return rr;
    }
     public static long str2long0(String str) {
        long rr = 0;
        try {
            rr = Long.parseLong(str);
        } catch (Exception e) {
            // rr=-111111;
            rr = 0;
        }
        return rr;
    }
         public static int str2int(String str,int def) {
        int rr = 0;
        try {
            rr = Integer.parseInt(str,10);
        } catch (Exception e) {
            // rr=-111111;
            rr = def;
        }
        return rr;
    }
    public static long str2long(String str,long def) {
        long rr = 0;
        try {
            rr = Long.parseLong(str);
        } catch (Exception e) {
            // rr=-111111;
            rr = def;
        }
        return rr;
    }
    public static Double str2double(String str) {
        
        try {
            double rr = Double.valueOf(str);
            return rr;
        } catch (Exception e) {
            return null;
        }
        
    }
    
    public static double str2double0(String str) {
        
        try {
            double rr = Double.valueOf(str);
            return rr;
        } catch (Exception e) {
            return 0;
        }
        
    }
  
    public static String converturl(String cs) {
        if (cs != null){
            cs = cs.replaceAll("&#", "a0_0a");

        }
        return cs;
    }
    
    public static String unconverturl(String cs) {
        if (cs != null){
            cs = cs.replaceAll("a0_0a", "&#");
            cs = cs.replaceAll("_00_", " ");

        }
        return cs;
    }
    

    //exception gettitle-10
    //exception -1
    //succes-0
    //warning-2
    //info-3
    //giorgi 1000
    //dave 2000
    //paata 3000
    //dima 4000
    //roma 5000
    //sarchi 6000
    public static void addLog(int type,String descrip) {
//        execFunction("oraAddLog",new String[] {String.format("%s",type),descrip}, false);
//        System.out.println(String.format("log---type=%d---%s",type,descrip));
    }
    
    	public static String filterNonAscii(String inString) {
		// Create the encoder and decoder for the character encoding
		Charset charset = Charset.forName("US-ASCII");
		CharsetDecoder decoder = charset.newDecoder();
		CharsetEncoder encoder = charset.newEncoder();
		// This line is the key to removing "unmappable" characters.
		encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
		String result = inString;

		try {
			// Convert a string to bytes in a ByteBuffer
			ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inString));

			// Convert bytes in a ByteBuffer to a character ByteBuffer and then to a string.
			CharBuffer cbuf = decoder.decode(bbuf);
			result = cbuf.toString();
		} catch (CharacterCodingException cce) {
			String errorMessage = "Exception during character encoding/decoding: " + cce.getMessage();
//			log.error(errorMessage, cce);
		}

		return result;	
	}
        
            public static int getmd5(String arg) {
        // TODO code application logic here
//System.out.println("md5 off="+arg);         
        int md5val = 0;
        MessageDigest algorithm = null;
        
        try {
            algorithm = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException nsae) {
            System.out.println("Cannot find digest algorithm");
            return 0;
//            System.exit(1);
        }
        
  
            byte[] defaultBytes = arg.getBytes();
            algorithm.reset();
            algorithm.update(defaultBytes);
            byte md[] = algorithm.digest();
            
            for (int i = 0; i < md.length; i++) {
    md5val+=java.lang.Math.abs(md[i]);            
//System.out.println(String.format("%d==%d==%d",i,md[i],md5val));      

            }
            md5val=(md5val % 900)+100;
//System.out.println(String.format("md5val==%d",md5val));              
            return md5val;
       
        
    } 
 

public static void str2file(String str,String filename) {
        try{
//        FileOutputStream ff=new FileOutputStream(filename);
            OutputStreamWriter ff=new OutputStreamWriter(new FileOutputStream(filename));
//            BufferedWriter ff=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
            ff.write(str);
            ff.close();
        }catch (Exception e) {System.out.println(e.toString());}
}
public static String file2str(String filename) {
        DataInputStream InStream;BufferedReader ff;
        String  ss,s1="";
        try {


            ff = new BufferedReader(new InputStreamReader(new FileInputStream(
                     filename)));
            while (true)
            {
                ss=ff.readLine();
                if (ss==null) break;
                s1+=ss+"\n";
            }
            ff.close();


        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return s1;
    }
public static String[] getfiles(String dirname) {

    File dir = new File(dirname);

return  dir.list();
}
public static boolean deletefile(String fname) {
    File dd = new File(fname);
    return dd.delete();
}
public static boolean renamefile(String fname,String fname2) {
    File dd = new File(fname);
    File dd2 = new File(fname2);
    return dd.renameTo(dd2);
}


public static String getmyip()
	{
   String ips="";
   try{
		Enumeration<NetworkInterface> e1 = (Enumeration<NetworkInterface>)NetworkInterface.getNetworkInterfaces();
		while(e1.hasMoreElements()) {
			NetworkInterface ni = e1.nextElement();
			ips+=ni.getName()+",";
//			System.out.print(ni.getName());
//			System.out.print(" : [");
			Enumeration<InetAddress> e2 = ni.getInetAddresses();
			while(e2.hasMoreElements()) {
				InetAddress ia = e2.nextElement();
			ips+=ia.toString()+",";
//				System.out.print(ia);
//				if( e2.hasMoreElements()) {
//					System.out.print(",");
//				}
			}
//			System.out.println("]");
		}
   }catch (Exception e){
       e.printStackTrace();
   }
   return ips;
	}
public static float[] str2floats(String str){
                if (str==null) str="";
                String[] ssm=str.split(",");
                float[] ff=new float[ssm.length];
                for (int i1=0;i1<ssm.length;i1++)
                        ff[i1]=(float)functions.str2double0(ssm[i1]);
                return ff;
}

public static String replaceprop(String dat,String key,
        String keyend,String val)
{//if (val.equals("")) return dat;
               int p1=dat.indexOf(key);
           int p2=-1;
           if (p1>-1)
             p2=dat.indexOf(keyend,p1+1);
           else dat="\n"+key+val+keyend+"\n"+dat;
           if (p2>p1)
               return dat.substring(0,p1)+key+val+keyend+dat.substring(p2+1);
           return dat;
}
//Set fonts = FontFactory.getRegisteredFonts();
//for (Object font : fonts) {
//    System.out.println(font);
//}


}
